<template>
  <div class="app-container">
    <el-form :inline="true" :model="form">
      <el-form-item label="关键词">
        <el-input v-model="form.keyWord" placeholder="关键词"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="loadSource">查询</el-button>
        <el-button type="primary" @click="handlerAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="keyWord" label="关键字" width="120"></el-table-column>
      <el-table-column prop="urlAddr" label="回复内容" width="400"></el-table-column>
      <el-table-column prop label="过期时间" width="120"></el-table-column>
      <el-table-column  label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleDel(scope.row.id)" type="text" size="small">删除</el-button>
          <el-button @click="handleEdit(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="资源管理" :visible.sync="dialogVisible" width="80%" :before-close="handleClose">
      <el-form :model="editForm">
        <el-form-item label="关键词">
          <el-input v-model="editForm.keyWord" placeholder="关键词"></el-input>
        </el-form-item>
        <el-form-item label="回复内容">
          <el-input v-model="editForm.urlAddr" placeholder="回复内容"></el-input>
        </el-form-item>
        <el-form-item label="过期时间">
          <el-input v-model="editForm.exprTime" placeholder="过期时间"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="addSource">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
       tableData: [],
      form: {
        keyWord: ''
      },
      editForm:{
        keyWord:'',
        urlAddr:'',
        exprTime:''
      },
      dialogVisible:false
    }
  },
  methods: {
    handleDel(rowId) {
         request({
        url: "/admin/source/del/"+rowId,
        method: "get",
        params: ""
      }).then(res => {
        this.loadSource()
      });
      },
    handlerAdd() {
      this.dialogVisible= true;
    },
    handleEdit(row){
      this.editForm = row;
      this.dialogVisible= true;
    },
    addSource(){
      request({
        url: "/admin/source/put",
        method: "post",
        data: this.editForm
      }).then(res => {
        this.loadSource();
        this.editForm = {};
        this.dialogVisible=false;
      });
    },
    handleClose(){
      this.editForm ={},
      this.dialogVisible=false;
    },
    onCancel() {
      this.$message({
        message: 'cancel!',
        type: 'warning'
      })
    },
    loadSource() {
      request({
        url: "/admin/source/list",
        method: "post",
        data: this.form
      }).then(res => {
        this.tableData = res.data;
      });
    }
  },
   created() {
    this.loadSource();
  },
}
</script>

<style scoped>
.line {
  text-align: center;
}
</style>

